<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['lkshop:relation1:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="relation1List">
      <el-table-column type="expand">
        <template #default="props">
          <div class="specs-container">
            <div class="specs-title">规格值：</div>
            <div v-if="props.row.specsValue && props.row.specsValue.trim()" class="specs-tags">
              <el-tag 
                v-for="(specValue, index) in props.row.specsValue.split(',').filter(val => val.trim())" 
                :key="index" 
                type="primary"
                effect="light"
                class="spec-tag"
              >
                {{ specValue.trim() }}
              </el-tag>
            </div>
            <div v-else class="no-specs">
              <el-empty description="暂无规格值" :image-size="60" />
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品ID" align="center" prop="goodsId" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="商品状态" align="center" prop="goodsStatus">
        <template #default="scope">
          <el-tag :type="scope.row.goodsStatus === 1 ? 'success' : 'danger'">
            {{ scope.row.goodsStatus === 1 ? '上架' : '下架' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="规格名称" align="center" prop="goodsSpecName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row)" v-hasPermi="['lkshop:relation1:query']">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看商品规格关联详情对话框 -->
    <el-dialog :title="detailTitle" v-model="detailOpen" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="商品名称">{{ detailForm.goodsName }}</el-descriptions-item>
        <el-descriptions-item label="商品状态">
          <el-tag :type="detailForm.goodsStatus === 1 ? 'success' : 'danger'">
            {{ detailForm.goodsStatus === 1 ? '上架' : '下架' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="规格名称">{{ detailForm.goodsSpecName }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ detailForm.createTime }}</el-descriptions-item>
      </el-descriptions>
      
      <el-divider content-position="left">规格值列表</el-divider>
      <div v-if="detailForm.specsValue && detailForm.specsValue.trim()" class="specs-tags">
        <el-tag 
          v-for="(specValue, index) in detailForm.specsValue.split(',').filter(val => val.trim())" 
          :key="index" 
          type="primary"
          effect="light"
          class="spec-tag"
          style="margin-right: 8px; margin-bottom: 8px;"
        >
          {{ specValue.trim() }}
        </el-tag>
      </div>
      <div v-else class="no-specs">
        <el-empty description="暂无规格值" :image-size="60" />
      </div>
      
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="detailOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Relation1">
import { listRelation1, getRelation1DetailWithSku } from "@/api/lkshop/relation1"

const { proxy } = getCurrentInstance()

const relation1List = ref([])
const detailOpen = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const detailTitle = ref("")

const data = reactive({
  detailForm: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    goodsName: null
  }
})

const { queryParams, detailForm } = toRefs(data)

/** 查询商品规格关联列表 */
function getList() {
  loading.value = true
  listRelation1(queryParams.value).then(response => {
    relation1List.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 查看按钮操作 */
function handleView(row) {
  const _id = row.id
  // 使用新的API从SKU表获取商品对应的具体规格值
  getRelation1DetailWithSku(_id).then(response => {
    detailForm.value = response.data
    detailOpen.value = true
    detailTitle.value = "查看商品规格关联详情（SKU规格值）"
  })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('lkshop/relation1/export', {
    ...queryParams.value
  }, `relation1_${new Date().getTime()}.xlsx`)
}

getList()
</script>

<style scoped>
.specs-container {
  padding: 20px;
  background-color: #f8f9fa;
  border-radius: 8px;
  margin: 10px;
}

.specs-title {
  font-size: 14px;
  font-weight: 500;
  color: #606266;
  margin-bottom: 12px;
}

.specs-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.spec-tag {
  font-size: 13px;
  padding: 6px 12px;
  border-radius: 16px;
  transition: all 0.3s ease;
}

.spec-tag:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.no-specs {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}
</style>
